//
//  RxSwiftController.swift
//  TestAPIManager
//
//  Created by 杨庆 on 2020/10/1.
//  Copyright © 2020 杨庆. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class RxSwiftController: UIViewController {
    
    let disposeBag = DisposeBag()
    var timer:Observable<Int>?
    
    @IBOutlet weak var popButton: UIButton!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 退出界面
        popButton.rx.tap
            .subscribe(onNext:{[weak self ] in
                self?.dismiss(animated: true , completion: nil)
            })
            .disposed(by: disposeBag)
        
        // 1. 创建序列
        _ = Observable<String>.create { (observabler) -> Disposable in

            // 3. 发送信号 -> 产生一个元素
            observabler.onNext("Cooci - 框架班级")
            observabler.onNext("发出这个字符串")
            return Disposables.create() // 这个销毁不影响我们这次解读
            
            // 2.订阅序列
        }.subscribe(onNext:{(text) in
            print("订阅到 - \(text)")
        })
    }
    
}

extension RxSwiftController {
    
    func rx_UIKit() {
        // 按钮响应
        rx_UIButton()
        
        // 文本框输入监听
        rx_textField()
        
        // scrollView 滚动视图
        rx_scrollView()
        
        // 键盘弹起通知
        rx_notiKeyboard()
        
        // 针对手势 tap 点击标签
        rx_labelTapGes()
        
        // timer 定时器
        setupTimer()
        
    }
    
    
    func rx_UIButton() {
        
        // 按钮点击
        self.popButton.rx.tap
            /* onNext 动作调用每个可观察序列中的每个元素 */
            .subscribe(onNext: { [weak self] in
                print("点了,小鸡炖蘑菇")
                self?.view.backgroundColor = UIColor.orange
            })
            .disposed(by: disposeBag)
    }
    
    // textfield 输入监听
    func rx_textField() {
        // textfield 文本响应
        self.textField.rx.text.orEmpty.changed
            .subscribe (onNext: { (text) in
                print("监听到了_\(text)")
            })
            .disposed(by: disposeBag)
        
        // 绑定到 popButton 的 title 属性

        /*
         self.popButton.rx.title()
         相当于button.setTitle:for: */
        self.textField.rx.text.bind(to: self.popButton.rx.title()).disposed(by: disposeBag)

    }
    
    // scrollView 滚动监听
    func rx_scrollView() {
        scrollView.rx.contentOffset.subscribe (onNext:{[weak self] (content) in
            self?.view.backgroundColor = UIColor.init(red: content.y/255.0*0.8, green: content.y/255.0*0.3, blue: content.y/255.0*0.6, alpha: 0.7);
            print(content.y)
        })
            .disposed(by: disposeBag)
    }
    
    // 监听键盘弹出
    func rx_notiKeyboard() {
        NotificationCenter.default.rx
            .notification(UIResponder.keyboardWillShowNotification)
            .subscribe { (event) in
                print("事件信息:\(event)")
        }.disposed(by: disposeBag)
    }
    
    // label 手势点击
    func rx_labelTapGes(){
        let tap = UITapGestureRecognizer()
        self.label.addGestureRecognizer(tap)
        self.label.isUserInteractionEnabled = true
        tap.rx.event.subscribe { (event) in
            print("点击了标签 label")
        }.disposed(by: disposeBag)
        
    }
    
    // 创建定时器
    func setupTimer() {
        timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        timer!.subscribe(onNext: { (num) in
            print("hello word \(num)")
        }).disposed(by: disposeBag)
    }
    
}
